package cn.com.elite;

import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

public class EliteBean {
	private static final Log log=LogFactory.getLog(EliteBean.class);
    private DataSource dataSource;
    private DataSourceTransactionManager tran;
    private PlatformTransactionManager transactionManager;
    public static String[] a;
    public static String sql123;
    
    
	public static String[] getA() {
		return a;
	}
	public static void setA(String[] a) {
		EliteBean.a = a;
	}
	public static String getSql123() {
		return sql123;
	}
	public static void setSql123(String sql123) {
		EliteBean.sql123 = sql123;
	}
	public DataSource getDataSource() {
		return dataSource;
	}
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	public DataSourceTransactionManager getTran() {
		return tran;
	}
	public void setTran(DataSourceTransactionManager tran) {
		this.tran = tran;
	}
	public PlatformTransactionManager getTransactionManager() {
		return transactionManager;
	}
	public void setTransactionManager(PlatformTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}
    
	public List chaxun(String sql) throws Exception{
		 try{
			 log.info("chaxun_sql:"+sql);  
			 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		     List resultList = jdbcTemplate.queryForList(sql);
		     return resultList;
		 }catch(Exception e){
			 log.error("chaxun错误:"+sql);  
			 log.error("chaxun错误:"+EliteUtil.getERRO(e));  
			 return null;
		 }
	 }
    
	public int update(String sql)throws Exception{
		try{
			 log.info("update_sql:"+sql);  
			 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		     int resultList=jdbcTemplate.update(sql);
		     return resultList;
		 }catch(Exception e){
			 log.error("update错误:"+sql);  
			 log.error("update错误:"+EliteUtil.getERRO(e));  
			 return 0;
		 }
	}
    private String getcontent(Object[] a){
    	String b="";
    	if(a!=null&&a.length>0){
    		for(int i=0;i<a.length;i++){
    			if(a[i]!=null){
    				b=b+"a["+i+"]："+a[i].toString()+"-";	
    			}else{
    				b=b+"a["+i+"]：null-";	
    			}
    		}
    	}
    	return b;
    }
    private String getcontent1(int[] a){
    	String b="";
    	if(a!=null&&a.length>0){
    		for(int i=0;i<a.length;i++){
    			b=b+"a["+i+"]："+a[i]+"-";
    		}
    	}
    	return b;
    }
	public String insertPersonUseExecute(String sql,int[] types,Object[] params){
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		try{
			int result=jdbcTemplate.update(sql,params,types);
			return result+"";
		}catch(Exception e){
			 log.error("出错types："+getcontent1(types));
			 log.error("出错params："+getcontent(params));
			 log.error("出错SQL："+sql);
			 log.error("insertPersonUseExecute出错:"+EliteUtil.getERRO(e));  
			 return "0";  
		}
		
	}
    public String chaxunString(String sql) throws Exception {       
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String resultList="";
        try{
        	resultList=(String)jdbcTemplate.queryForObject(sql,java.lang.String.class);
        }catch(Exception e){
        	log.error(sql+":"+e);
        }
        return resultList;
   }
    public int count(String sql) throws Exception {      
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    	int result=0;
    	try{
    		result=jdbcTemplate.queryForInt(sql);
    	}catch(Exception e){
    		log.error(sql+":"+e);
    	}
    	log.info("count_result:"+result);
        return result;
    }
    public void insertUser1(){
    	try{
    		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    		for(int i=0;i<a.length;i++){
    			int resultList = jdbcTemplate.update(a[i]);
    			log.info(a[i]+"--"+resultList);
    		}
    		int resultList1 = jdbcTemplate.update(sql123);
    		log.info(sql123+"--"+resultList1);
    	}catch(Exception e){
    		log.info("insertUser1:"+e.toString());
    	}
    }
}
